package com.example.book.crawler.controller;

import com.example.book.base.BaseController;
import com.example.book.base.Result;
import com.example.book.common.utils.RedisService;
import com.example.book.crawler.service.CrawlerContentService;
import com.example.book.in.entity.BookInfo;
import com.example.book.in.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import com.example.book.crawler.entity.CrawlerCatalog;
import com.example.book.crawler.service.CrawlerCatalogService;


/**
 * @author hcj
 * @date 2019-11-01 22:25:02
 */
@Controller
@RequestMapping(value = "/sys/crawlerCatalog")
public class CrawlerCatalogController extends BaseController {

    @Autowired
    private CrawlerCatalogService crawlerCatalogService;
    @Autowired
    private BookInfoService bookInfoService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CrawlerContentService contentService;

    @ModelAttribute
    public CrawlerCatalog get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            CrawlerCatalog crawlerCatalog = new CrawlerCatalog();
            crawlerCatalog.setId(id);
            return crawlerCatalogService.get(crawlerCatalog);
        } else {
            return new CrawlerCatalog();
        }
    }

    /**
     * 新增修改页面
     *
     * @param crawlerCatalog
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(CrawlerCatalog crawlerCatalog, Model model) {
        if (crawlerCatalog != null) {
            model.addAttribute("crawlerCatalog", crawlerCatalog);
        }
        return "crawler/crawler_catalog_form";
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public CrawlerCatalog getById(CrawlerCatalog crawlerCatalog) {
        return crawlerCatalog;
    }

    @RequestMapping(value = "/list")
    public String list(CrawlerCatalog crawlerCatalog) {
        return "crawler/crawler_catalog_list";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<CrawlerCatalog> info(CrawlerCatalog crawlerCatalog) {
        return crawlerCatalogService.findList(crawlerCatalog);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<CrawlerCatalog> findPage(CrawlerCatalog crawlerCatalog) {
        PageInfo<CrawlerCatalog> pageInfo = crawlerCatalogService.findPage(crawlerCatalog);

        BookInfo bi = null;
        for (CrawlerCatalog cc : pageInfo.getList()) {
            bi = new BookInfo();
            bi.setId(cc.getBookInfoId());
            BookInfo bookInfo = bookInfoService.get(bi);
            if (bookInfo != null) {
                cc.setBookInfoId(bookInfo.getBookName());
            }
        }
        return pageInfo;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(CrawlerCatalog crawlerCatalog) {
        // 可初始化的状态
        crawlerCatalog.setIsCarriedOut("0");
        crawlerCatalogService.save(crawlerCatalog);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        crawlerCatalogService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        crawlerCatalogService.delete(ids);
        return Result.success();
    }

}